//
//  XWXSqliteQueryCommand+IUD.m
//  XWXSqlite
//
//  Created by 许卫星 on 17/9/5.
//  Copyright © 2017年 wxxu. All rights reserved.
//

#import "XWXSqliteQueryCommand+IUD.h"
#import "XWXSqliteQueryCommand+Query.h"
#import "NSString+XWXSqliteSQL.h"
#import "XWXSqliteConfiguration.h"

@implementation XWXSqliteQueryCommand (IUD)

- (XWXSqliteQueryCommand *)insertTable:(NSString *)tableName columDataDic:(NSDictionary *)columDataDic
{
    [self resetQueryCommand];
    
    NSString *safeTableName = [tableName safeSQLMetaString];
    if (kXWXSqlite_isEmptyString(safeTableName) || columDataDic == nil) {
        return self;
    }
    
    NSString *columNameString = [[[columDataDic allKeys] componentsJoinedByString:@","] safeSQLMetaString];
    NSString *columValueString = [[[columDataDic allValues] componentsJoinedByString:@","] safeSQLEncode];
    
    [self.sqlString appendFormat:@"INSERT INTO %@ (%@) VALUES (%@);", safeTableName, columNameString, columValueString];
    
    return self;
}

//[NSString stringWithFormat:@"insert into %@ (%@) select %@ from %@;",tmpTableName, primaryKey, primaryKey, tableName]
- (XWXSqliteQueryCommand *)insertSelectTable:(NSString *)tableName fromTableName:(NSString *)fromTableName intoColums:(NSArray *)intoColums selectColums:(NSArray *)selectColums
{
    [self resetQueryCommand];
    
    NSString *safeTableName = [tableName safeSQLMetaString];
    NSString *safefromTableName = [fromTableName safeSQLMetaString];
    if (kXWXSqlite_isEmptyString(safeTableName) || kXWXSqlite_isEmptyString(fromTableName) || intoColums.count == 0 || selectColums.count == 0 ) {
        return self;
    }
    
    NSString *intoColumNames = [[intoColums componentsJoinedByString:@","] safeSQLMetaString];
    NSString *selectColumNames = [[selectColums componentsJoinedByString:@","] safeSQLEncode];
    
    [self.sqlString appendFormat:@"INSERT INTO %@ (%@) SELECT %@ FROM %@;", safeTableName, intoColumNames, selectColumNames,safefromTableName];
    
    return self;
}

- (XWXSqliteQueryCommand *)updateTable:(NSString *)tableName columData:(NSDictionary *)columDataDic whereCondition:(NSString *)condition
{
    [self resetQueryCommand];
    
    NSString *safeTableName = [tableName safeSQLMetaString];
    if (kXWXSqlite_isEmptyString(safeTableName) || columDataDic == nil){
        return self;
    }
    
    NSMutableArray *valueList = [[NSMutableArray alloc] init];
    
    [columDataDic enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull colum, NSString * _Nonnull value, BOOL * _Nonnull stop) {
        [valueList addObject:[NSString stringWithFormat:@"%@ = (%@)", [colum safeSQLMetaString],[value safeSQLEncode]]];
    }];
    
    NSString *valueString = [valueList componentsJoinedByString:@","];
    
    [self.sqlString appendFormat:@"UPDATE %@ SET %@ ", safeTableName, valueString];
    
    return [self where:condition];
}

- (XWXSqliteQueryCommand *)deleteTable:(NSString *)tableName whereCondition:(NSString *)condition
{
    [self resetQueryCommand];
    
    NSString *safeTableName = [tableName safeSQLMetaString];
    if (kXWXSqlite_isEmptyString(safeTableName)){
        return self;
    }
    
    [self.sqlString appendFormat:@"DELETE FROM %@ ", safeTableName];
    
    return [self where:condition];
}
@end
